"""
关键字驱动类的封装，主要用于做Selenium常用操作行为的提取与二次封装：
1.get
2.send keys
3.click
4.find element
...
"""
from selenium import webdriver

def open_browser(_type):
    try:
        driver = getattr(webdriver, _type)()
    except:
        driver = webdriver.Chrome()
    return driver

class Keys:
    # 创建一个浏览器对象
    def __init__(self, _type):
        self.driver = open_browser(_type)

    # 访问URL
    def open(self, url):
        self.driver.get(url)

    # 元素定位
    def locator(self, by, value):
        return self.driver.find_element(by, value)

    # 输入
    def input(self, by, value, text):
        self.locator(by, value).send_keys(text)

    # 点击
    def click(self, by, value):
        self.locator(by, value).click()

    # 退出浏览器
    def quit(self):
        self.driver.quit()

    # 切换句柄
    def switch_window(self, close=None, num=1):
        handles = self.driver.window_handles
        if close is not None:
            self.driver.close()
        self. driver.switch_to.window(handles[num])

    # 隐式等待
    def sleep(self, time):
        self.driver.implicitly_wait(time)

    def window_max(self):
        self.driver.maximize_window()